package cn.tedu._03vehicle.base.interceptors;

import cn.tedu._03vehicle.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
public class LoginCheckInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("进入权限校验拦截器...");
        /*
            1.获取会话参数HttpSession;
              HttpSession session = request.getSession();
            2.获取会话标识getAttribute("user");
            3.判断
              3.1 登录: 放行;
              3.2 未登录: 重定向到登录页面 /login.html, return false;
                  response.sendRedirect("/login.html");
         */
        HttpSession session = request.getSession();
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            response.sendRedirect("/login.html");
            return false;
        }
        return true;//放行
    }
}
